package com.kgc.itrip.service.impl;

import com.kgc.itrip.common.Constants;
import com.kgc.itrip.common.EmptyUtils;
import com.kgc.itrip.common.Page;
import com.kgc.itrip.mapper.ItripCommentMapper;
import com.kgc.itrip.mapper.ItripImageMapper;
import com.kgc.itrip.pojo.ItripComment;
import com.kgc.itrip.pojo.ItripImage;
import com.kgc.itrip.pojo.ItripUser;
import com.kgc.itrip.service.ItripCommentService;
import com.kgc.itrip.vo.comment.ItripAddCommentVO;
import com.kgc.itrip.vo.comment.ItripListCommentVO;
import com.kgc.itrip.vo.comment.ItripScoreCommentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ItripCommentServiceImpl implements ItripCommentService {

    @Resource
    private ItripCommentMapper itripCommentMapper;
    @Resource
    private ItripImageMapper itripImageMapper;

    public ItripComment getItripCommentById(Long id)throws Exception{
        return itripCommentMapper.getItripCommentById(id);
    }

    /*public List<ItripComment> getItripCommentListByMap(Map<String,Object> param)throws Exception{
        return itripCommentMapper.getItripCommentListByMap(param);
    }*/

    public Integer getItripCommentCountByMap(Map<String,Object> param)throws Exception{
        return itripCommentMapper.getItripCommentCountByMap(param);
    }

    public Integer itriptxAddItripComment(ItripAddCommentVO commentVo, ItripUser itripUser)throws Exception{
        ItripComment comment = new ItripComment();
        BeanUtils.copyProperties(commentVo,comment);
        comment.setUserId(itripUser.getId());
        comment.setScore((comment.getFacilitiesScore()+comment.getHygieneScore()+
                comment.getPositionScore()+comment.getServiceScore()) / 4);
        comment.setTripMode(Long.parseLong(commentVo.getTripMode()));
        comment.setCreatedBy(itripUser.getId());
        comment.setCreationDate(new Date());
        //调用数据访问层添加评论对象
        Integer num = itripCommentMapper.insertItripComment(comment);

        if(commentVo.getIsHavingImg()==1){
            ItripImage[] itripImages = commentVo.getItripImages();
            for (int i=0; i<itripImages.length;i++){
                ItripImage itripImage = itripImages[i];
                itripImage.setTargetId(comment.getId());
                itripImage.setCreationDate(new Date());
                itripImage.setCreatedBy(itripUser.getId());
                itripImageMapper.insertItripImage(itripImage);
            }
        }
        return num;
    }

    public Integer itriptxModifyItripComment(ItripComment itripComment)throws Exception{
        itripComment.setModifyDate(new Date());
        return itripCommentMapper.updateItripComment(itripComment);
    }

    public Integer itriptxDeleteItripCommentById(Long id)throws Exception{
        return itripCommentMapper.deleteItripCommentById(id);
    }

    public Page<ItripListCommentVO> queryItripCommentPageByMap(Map<String,Object> param, Integer pageNo, Integer pageSize)throws Exception{
        Integer total = itripCommentMapper.getItripCommentCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo)||pageNo==0 ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize)||pageSize==0 ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<ItripListCommentVO> itripCommentList = itripCommentMapper.getItripCommentListByMap(param);
        page.setRows(itripCommentList);
        return page;
    }

    @Override
    public ItripScoreCommentVO queryItripHotelScore(Long hotelId) throws Exception {
        ItripScoreCommentVO itripScoreCommentVO = itripCommentMapper.queryItripHotelScoreByHotelId(hotelId);
        return itripScoreCommentVO;
    }

}
